﻿// ===============================================================================
// Copyright(c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:马星俊
// ===============================================================================

using DevExpress.Utils;
using System.Collections.Generic;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Helper;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Windows;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统机构服务
    /// </summary>
    public class SysDeptService
    {
        private const string RootUrl = "/api/sys/dept";
        public static SysDeptService Instance { get; } = new SysDeptService();

        public SvgImageCollection GetDeptImageCollection()
        {
            var imageCollection1 = new DevExpress.Utils.SvgImageCollection();
            if (WinFormHelper.IsDesignMode()) return imageCollection1;
            imageCollection1.Insert(0, "bo_category", R.DeptTreeSvgImage);
            return imageCollection1;
        }

        /// <summary>
        /// 新建机构模型
        /// </summary>
        /// /// <param name="parentId">上级机构id</param>
        public async Task<SysDept> NewAsync(string parentId)
        {
            var model = new SysDept
            {
                Status = true
            };
            model.ParentId = WinFormHelper.GetParentIdOrDefault(parentId);
            return await Task.FromResult(model);
        }

        /// <summary>
        /// 复制机构模型
        /// </summary>
        /// <param name="id">机构主键</param>
        public async Task CopyAsync(string id)
        {
            var model = await SelectByIdAsync(id);
            model.Id = null;
            model.Spell = null;
            model.Code += " - 复制";
            model.Name += " - 复制";
            await SaveAsync(true, model);
        }

        /// <summary>
        /// 保存机构
        /// </summary>
        /// <param name="created">是否新建</param>
        /// <param name="model">机构模型对象</param>
        public async Task<BoolResult> SaveAsync(bool created, SysDept model)
        {
            return await App.Client.PostAsBoolResultAsync($"{RootUrl}/{(created ? "insert" : "update")}", model);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <param name="status">状态,1启用;0禁用</param>
        /// <returns>成功返回true</returns>
        public async Task UpdateStatusAsync(string ids, bool status)
        {
            await App.Client.PostAsync($"{RootUrl}/updateStatus", new { ids, status });
        }

        /// <summary>
        /// 保存节点拖拽数据
        /// </summary>
        /// <param name="keyValues">修改的数据集合</param>
        /// <returns>成功返回true</returns>
        public async Task UpdateDndAsync(List<PrimaryKeyValue> keyValues)
        {
            await App.Client.PostAsync($"{RootUrl}/updateNodeDrag", WinFormHelper.ToTreeNodeDnds(keyValues));
        }

        /// <summary>
        /// 删除机构
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public async Task DeleteAsync(string ids)
        {
            await App.Client.PostAsync($"{RootUrl}/delete", new { ids });
        }

        /// <summary>
        /// 根据主键获取机构对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<SysDept> SelectByIdAsync(string id)
        {
            return await App.Client.PostAsync<SysDept>($"{RootUrl}/selectById", new { id });
        }

        /// <summary>
        /// 根据主键获取机构对象
        /// </summary>
        /// <param name="args">编辑参数</param>
        public Task<SysDept> GetOrCreateAsync(EditFormArgs args)
        {
            if (args == null || args.Created) return NewAsync(args?.ParentId);
            return SelectByIdAsync(args.Id);
        }

        /// <summary>
        /// 查询机构列表
        /// </summary>
        /// <param name="filter">过滤条件</param>
        public async Task<List<SysDept>> SelectListAsync(SysDeptFilter filter)
        {
            return await App.Client.PostAsync<List<SysDept>>($"{RootUrl}/selectList", filter);
        }

        /// <summary>
        /// 导出机构列表
        /// </summary>
        /// <param name="filePath">保存路径</param>
        /// <param name="filter">过滤条件</param>
        public async Task ExportAsync(string filePath, SysDeptFilter filter)
        {
            await App.Client.DownloadAsync($"{RootUrl}/export", filePath, filter);
        }

        /// <summary>
        /// 刷新服务端机构缓存
        /// </summary>
        public async Task RefreshCacheAsync()
        {
            await App.Client.PostAsync($"{RootUrl}/refreshCache");
        }
    }
}